package com.masterclient.app.modules.shop.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.modules.shop.beans.DealerBean;
import com.masterclient.app.modules.shop.beans.DealerItemBean;


public class DealerToolsAdapter extends BaseQuickAdapter<DealerBean.ToolsAdvBean, BaseViewHolder> {

    private int itemType;

    public DealerToolsAdapter() {
        super(R.layout.item_dealer_bench_tools);
        itemType = 0;
    }

    public void setType(int itemType) {
        this.itemType = itemType;
        notifyDataSetChanged();
    }

    @Override
    protected void convert(BaseViewHolder helper, DealerBean.ToolsAdvBean item) {

        helper.setText(R.id.item_dealer_bench_tools_title,item.title)
                .setText(R.id.item_dealer_bench_tools_subtitle, item.subTitle)
                .setGone(R.id.item_dealer_bench_tools_line,itemType==1&&helper.getAdapterPosition()!=getItemCount()-1);

        RecyclerView recyclerView = helper.getView(R.id.item_dealer_bench_tools_recycler);

        if (item.items != null && item.items.size()>0) {
            switch (itemType) {
                case 0:
                    recyclerView.setPadding(ScreenUtils.dp2px(16), ScreenUtils.dp2px(5), ScreenUtils.dp2px(7), 0);
                    recyclerView.setLayoutManager(new GridLayoutManager(mContext, 2) {
                        @Override
                        public boolean canScrollVertically() {
                            return false;
                        }
                    });
                    break;
                case 1:
                    recyclerView.setPadding(ScreenUtils.dp2px(6), ScreenUtils.dp2px(6), ScreenUtils.dp2px(6), ScreenUtils.dp2px(helper.getAdapterPosition() == getItemCount()-1?8:18));
                    recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4) {
                        @Override
                        public boolean canScrollVertically() {
                            return false;
                        }
                    });
                    break;
            }
            for (DealerItemBean childItem : item.items) {
                childItem.setItemType(itemType);
            }
            DealerToolsChildAdapter adapter = new DealerToolsChildAdapter(item.items);
            adapter.bindToRecyclerView(recyclerView);
            recyclerView.setVisibility(View.VISIBLE);
        }else {
            recyclerView.setVisibility(View.GONE);
        }
    }
}
